<?php
class Sysset extends MY_Controller
{
	public function __construct(){
		parent::__construct();
		$this->_data = array();
	}
	
	public function index(){		
		// if($this->input->get('action') == 'ok'){
		// 	die('-----------');
		// }

		$this->_data['list_current'] = array('sysset','sysset');		

		$o = $this->db->query("SELECT * FROM {$this->db->dbprefix}sysset WHERE domain='".SITE_DOMAINNAME."'");
		
		$data = $o->result();
		if($data && $data[0]){
			$data = $data[0];
		}else{
			$data = array();
		}
		$this->_data['data'] = $data;

		if($this->input->get('ajax')){
			echo json_encode($data);exit();
		}
		
		$this->load->view('admin/header', $this->_data);
		$this->load->view('admin/sysset', $this->_data);
		$this->load->view('admin/footer');
	}
	function add(){
		if($this->input->post('domain')){
			$clientqq = trim($this->input->post('clientqq'));

		// echo "f "; echo $this->input->post('clientqq');

			if($clientqq){
				$arr_cq = explode(' ', $clientqq);
				$arr_cq1 = array();
				foreach ($arr_cq as $key => $value) {
					if($value){
						array_push($arr_cq1,$value);
					}
				}
				$clientqq = implode(',', $arr_cq1);
			}

			$images = $this->input->post('indexpageimages');
			$new_images = array();
			if($images){
				$new_images = explode(',', $images);
			}
			$des = './data/indexpageimages';
			if(!file_exists($des)){
				mkdir($des);
			}

			$des .= '/';
			for($i=0; $i<4; $i++){
				if($_FILES['image'.$i]){
					// if(move_uploaded_file(, destination))
					// $new_images[$i] = $_FILES['image'.$i];

					$file_ext = end(explode('.', $_FILES['image'.$i]['name']));
					if(!in_array($file_ext, array('jpg','gif','png','bmp'))) continue;
					$file_name = date("YmdHis").'_'.$i . '.'.$file_ext;
					if(move_uploaded_file($_FILES['image'.$i]['tmp_name'], $des.$file_name)){
						$new_images[$i] = $file_name;
					}else{
						$new_images[$i] = '';
					}
				}
			}
			// var_dump($new_images);exit();
			
			/*
			
			$config['upload_path'] = './data/indexpageimages/';
		    $config['allowed_types'] = 'gif|jpg|png';
		    $config['max_size'] = '1000';
		    $config['max_width']  = '1024';
		    $config['max_height']  = '768';
		    $config['file_name'] = time();
		    $this->load->library('upload', $config);

		    for($i=0; $i<4; $i++){
		    	if($this->upload->do_upload('image'.$i)){
		    		
		    		//$new_images[$i] = substr($config['upload_path'], 1) . $img['file_name'];
		    		$img = $this->upload->data();
		    		$new_images[$i] = $img['file_name'];
		    	}else{
		    		//echo $this->upload->display_errors();exit();
		    		continue;
		    	}
		    } // endof for 		  
// print_r($img);exit();
*/		    foreach ($new_images as $key => $value) {
		    	if(!$value){
		    		unset($new_images[$key]);
		    	}
		    }
		    $images = implode(',', $new_images);

		    $bottomlinks = $this->input->post('bottomlinks');
			$setarr = array(
				'clientqq'=>$clientqq,
				'indexpageimages' => $images,
				'bottomlinks' => $bottomlinks,
				'updatetime'=>date("Y-m-d H:i:s")
				);
			

			$this->db->where('domain', $this->input->post('domain'));
			$this->db->update('sysset', $setarr);
			redirect('sysadmin/sysset?action=ok','refresh');
		}else{
			$this->index();			
		}
	}
	
}